from django.shortcuts import render, redirect
from app01 import models


# Create your views here.
def login(request):
    if request.method == "POST":
        # 处理post请求逻辑
        # 获取用户名和密码
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        # 进行校验
        # if user == 'liangliang' and pwd == '123':
        if models.User.objects.filter(username=user, password=pwd):
            # 校验成功，告知登录成功
            # return HttpResponse('登录成功')
            # 选择跳转
            return redirect('/index/')

    return render(request, 'login.html')
    # return render(request, 'loginl.html')


def index(request):
    # orm测试
    # ret = models.User.objects.all()  # QuerySet对象列表，每个元素是一个对象
    # for i in ret:
    #     print(i, i.username, i.password)

    # ret = models.User.objects.get(username='liangliang', password='123')  # 获取符合条件的对象
    # ret = models.User.objects.get(username='liangliang')  # 获取一条数据，获取不到&获取多条就报错
    ret = models.User.objects.filter(password='123')  # 获取满足条件的对象,或对象列表
    print(ret, type(ret))

    return render(request, 'index.html')
